package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.core.Pager;
import com.campus.mapper.ManagerClassMapper;
import com.campus.mapper.TeachingStudentInClassMapper;
import com.campus.mapping.ManagerClassMapping;
import com.campus.mapping.TeachingStudentClassMapping;
import com.campus.model.TManagerClass;
import com.campus.model.TTeachingStudentInClass;
import com.campus.model.dto.ManagerActivityUpdateDto;
import com.campus.model.dto.ManagerClassConditionDto;
import com.campus.model.dto.ManagerClassCreateDto;
import com.campus.model.dto.ManagerClassUpdateDto;
import com.campus.model.param.AddClassStudentParam;
import com.campus.model.param.AddClassStudentsParam;
import com.campus.model.vo.ClassPageVo;
import com.campus.service.IManagerClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ManagerClassServiceImpl extends ServiceImpl<ManagerClassMapper, TManagerClass>
        implements IManagerClassService {


    @Autowired
    private ManagerClassMapper managerClassMapper;

    @Autowired
    private TeachingStudentInClassMapper teachingStudentInClassMapper;


    public Integer createClass(ManagerClassCreateDto managerClassCreateDto){
        TManagerClass tManagerClass = ManagerClassMapping.INSTANCE.CreatetoClass(managerClassCreateDto);
        return managerClassMapper.insert(tManagerClass);
    }

    public Integer updateClass(ManagerClassUpdateDto managerClassUpdateDto){
        TManagerClass tManagerClass = ManagerClassMapping.INSTANCE.UpdatetoClass(managerClassUpdateDto);
        return managerClassMapper.updateById(tManagerClass);
    }

    @Override
    public Page<ClassPageVo> getClassListByConditions(ManagerClassConditionDto managerClassConditionDto, Pager pager) {

        QueryWrapper<TManagerClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(managerClassConditionDto.getName()),"t1.`name`",managerClassConditionDto.getName());
        queryWrapper.like(StringUtils.isNotBlank(managerClassConditionDto.getTeacherName()),"t2.login_name",
                managerClassConditionDto.getTeacherName());
        queryWrapper.like(StringUtils.isNotBlank(managerClassConditionDto.getActivityName()),"t3.`name`",
                managerClassConditionDto.getActivityName());

        Page<ClassPageVo> page = new Page<>(pager.getPageNum(),pager.getPageSize());
        return managerClassMapper.page(page,queryWrapper);
    }

    @Override
    public Integer addClassTeacher(Long classId, Long teacherId) {
        TManagerClass tManagerClass = managerClassMapper.selectById(classId);
        tManagerClass.setTeacherId(teacherId);

        return managerClassMapper.updateById(tManagerClass);
    }

    @Override
    @Transactional
    public Integer addClassStudents(AddClassStudentsParam addClassStudentsParam) {

        List<Long> studentIds = addClassStudentsParam.getStudentIds();
        Integer flag = 0;
        for (Long studentId : studentIds) {

            TTeachingStudentInClass tTeachingStudentInClass = new TTeachingStudentInClass();
            tTeachingStudentInClass.setClassId(addClassStudentsParam.getClassId());
            tTeachingStudentInClass.setStudentId(studentId);
            flag = teachingStudentInClassMapper.insert(tTeachingStudentInClass);
            if (flag == 0){
                throw new RuntimeException("添加失败");
            }
        }

        return flag;
    }

    @Override
    public Integer addClassStudent(AddClassStudentParam addClassStudentParam) {
            TTeachingStudentInClass tTeachingStudentInClass =
                    TeachingStudentClassMapping.INSTANCE.CreateParamtoEntity(addClassStudentParam);
            Integer flag = teachingStudentInClassMapper.insert(tTeachingStudentInClass);
            return flag;
    }

    @Override
    public Long TeacherClassCount(Long teacherId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("teacher_id",teacherId);
        Long count = managerClassMapper.selectCount(queryWrapper);
        return count;
    }

    //查找学生是否加入班级
    @Override
    public Boolean isJoinClass(Long studentId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("student_id",studentId);
        Long count = teachingStudentInClassMapper.selectCount(queryWrapper);
        return count > 0;

    }

    @Override
    public Boolean isJoinClass(List<Long> studentId) {
        for (Long id : studentId) {
            Long joinClass = isJoinClass(id) ? 1l : 0;
            if (joinClass > 0) { //如果大于0，说明已经加入班级
                return false;
            }
        }
        return true;
    }
}
